package com.guangzhi.weijianzhi.mainhome;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alipay.sdk.pay.demo.PayAllUtuils;
import com.alipay.sdk.pay.demo.PayDemoActivity;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.adapter.MainHotAdapter;
import com.guangzhi.weijianzhi.entity.MainHotTextEntity;
import com.guangzhi.weijianzhi.fragment.BaseFragment;
import com.guangzhi.weijianzhi.http.HttpRequestUtils;
import com.guangzhi.weijianzhi.utils.Misc;
import com.guangzhi.weijianzhi.view.ListViewForScrollView;
import com.loopj.android.http.AsyncHttpResponseHandler;
/**
 * 公用主界面
 * @author Administrator
 *
 */

public class MainHomeFragment extends BaseFragment implements OnClickListener, OnItemClickListener{
	private String city = "";
	private LocationClient mLocationClient = null;
	private BDLocationListener myListener = new MyLocationListener();
	private MainHotAdapter mAdapter;
	private ScrollView mSv;
	private ImageView image;
	private JSONArray jsonText = new JSONArray();
	private TextView prefecture_one,prefecture_two,prefecture_three,prefecture_four,prefecture_five,prefecture_six;
	private ArrayList<MainHotTextEntity> entitys = new ArrayList<MainHotTextEntity>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View mView = View.inflate(getActivity(), R.layout.main_activity_layout, null);
		initView(mView);
		initLocation();
		return mView;
	}
	
	private void initLocation() {
		// TODO Auto-generated method stub
		if("".equals(city) || null==city){
			mLocationClient = new LocationClient(getActivity()
					.getApplicationContext()); // 声明LocationClient类
			mLocationClient.registerLocationListener(myListener); // 注册监听函数
			setLocationOption();
			mLocationClient.start();// 开始定位

			if (mLocationClient != null && mLocationClient.isStarted())
				mLocationClient.requestLocation();	
		}else{
			goBack.setText(city);
		}
	}

	private void initView(View mView) {
		// TODO Auto-generated method stub
		initTitle(mView);
		goBack.setCompoundDrawables(null, null, null, null);
		goBack.setOnClickListener(this);
		titleTextV.setText("首页");
		titleRightBtn2.setText("发布");
		titleRightBtn2.setOnClickListener(this);
		mSv = (ScrollView) mView.findViewById(R.id.index_scrollview);
		mSv.scrollTo(0, 0); // 正常定位到起点
		mSv.smoothScrollTo(0, 0);

		   mView.findViewById(R.id.main_more).setOnClickListener(this);//更多
	       prefecture_one = (TextView) mView.findViewById(R.id.prefecture_one);
	       prefecture_one.setOnClickListener(this);
	       prefecture_two = (TextView) mView.findViewById(R.id.prefecture_two);
	       prefecture_two.setOnClickListener(this);
	       prefecture_three = (TextView) mView.findViewById(R.id.prefecture_three);
	       prefecture_three.setOnClickListener(this);
	       prefecture_four = (TextView) mView.findViewById(R.id.prefecture_four);
	       prefecture_four.setOnClickListener(this);
	       prefecture_five = (TextView) mView.findViewById(R.id.prefecture_five);
	       prefecture_five.setOnClickListener(this);
	       prefecture_six = (TextView) mView.findViewById(R.id.prefecture_six);
	       prefecture_six.setOnClickListener(this);
			ListViewForScrollView mListView = (ListViewForScrollView) mView.findViewById(R.id.main_list);
			mAdapter = new MainHotAdapter(getActivity());
			mListView.setAdapter(mAdapter);
			mListView.setOnItemClickListener(this);
			mAdapter.onBoundData(entitys);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		if(jsonText.length()==0){
			initTextList();
		}
		if(entitys.size()==0){
			intPostList();
		}
		
	}
	
	private void intPostList() {
		
		HttpRequestUtils.doHttpHotText("100000", 1, new AsyncHttpResponseHandler() {
			
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				
				try {
					JSONObject jsonO = new JSONObject(new String(arg2));
					if(jsonO.optBoolean("status")){
						entitys.clear();
						JSONArray optJSONArray = jsonO.optJSONArray("data");
						
						for(int j=0;j<optJSONArray.length();j++){
							MainHotTextEntity entity = new MainHotTextEntity();
							entity.title = optJSONArray.optJSONObject(j).optString("title");
							entity.areaString = optJSONArray.optJSONObject(j).optString("areaString");
							entity.unitString = optJSONArray.optJSONObject(j).optString("unitString");
							entity.applyNum = optJSONArray.optJSONObject(j).optString("applyNum");
							entity.work_start_time = optJSONArray.optJSONObject(j).optString("work_start_time");
							entity.sort_mark = optJSONArray.optJSONObject(j).optString("sort_mark");
							entity.id = optJSONArray.optJSONObject(j).optString("id");
							entitys.add(entity);
							mAdapter.onBoundData(entitys);
						}
						
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
				// TODO Auto-generated method stub
				Misc.alert("失败了");
			}
		});
		
		
	}

	private void initTextList() {
		// TODO Auto-generated method stub
		HttpRequestUtils.doHttpDateList(new AsyncHttpResponseHandler() {
			
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				try {
					JSONObject jsonO = new JSONObject(new String(arg2));
					if(jsonO.optBoolean("status")){
						JSONArray optJSONArray = jsonO.optJSONArray("data");
						jsonText =optJSONArray;
						prefecture_one.setText(optJSONArray.optJSONObject(0).optString("name"));
						prefecture_two.setText(optJSONArray.optJSONObject(1).optString("name"));
						prefecture_three.setText(optJSONArray.optJSONObject(2).optString("name"));
						prefecture_four.setText(optJSONArray.optJSONObject(3).optString("name"));
						prefecture_five.setText(optJSONArray.optJSONObject(4).optString("name"));
						prefecture_six.setText(optJSONArray.optJSONObject(5).optString("name"));
					}
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	/**
	 * 设置相关参数
	 */
	private void setLocationOption() {
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);
		option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
		option.setAddrType("all");// 返回的定位结果包含地址信息
		option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
		option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
		option.disableCache(true);// 禁止启用缓存定位
		option.setPoiNumber(5); // 最多返回POI个数
		option.setPoiDistance(1000); // poi查询距离
		option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息
		mLocationClient.setLocOption(option);
	}
	
	/**
	 * @author guangzhi 获取经纬度
	 */
	public class MyLocationListener implements BDLocationListener {
		

		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null) {
				return;
			}

			city = location.getCity();
			goBack.setText(city);

			String Area = location.getAddrStr();

			mLocationClient.stop();

		}

		@Override
		public void onReceivePoi(BDLocation arg0) {
			// TODO Auto-generated method stub

		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.title_back:
			 startActivity(new Intent(getActivity(),CitysSearchActivity.class));
			break;
		case R.id.prefecture_one://专区一
			startActivity(new Intent(getActivity(),com.example.sharesdk.MainActivity.class));
			break;
		case R.id.prefecture_two://二
			
			
			break;
		case R.id.prefecture_three://三
			
			break;
		case R.id.prefecture_four://四
			if(jsonText.length()>6) {
				String value = jsonText.optJSONObject(3).optString("value");
			}
			break;
		case R.id.prefecture_five://五
			break;
		case R.id.prefecture_six://六
			break;
			
		case R.id.title_right2://发布
			break;
		}
	}
	/*private void showShare() {
		final OnekeyShare oks = new OnekeyShare();
			oks.setText("( " + "来自微兼职," + "http://t.cn/RPd9QsI " + ")");
		oks.setNotification(R.drawable.logo, "微兼职");
		oks.setTitle("微兼职");
		oks.setUrl("http://www.weijianzhi.net");
		oks.setSilent(true);
	//	oks.setPlatform(null);
		oks.setDialogMode();
		oks.show(getActivity());
	}   
*/
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
	//startActivity(new Intent(getActivity(), PositionListFindActivity.class));
		//startActivity(new Intent(getActivity(), PositionDetailsActivity.class));
		
		Intent intent =new Intent(getActivity(), PositionDetailsActivity.class);
		intent.putExtra("jobid", entitys.get(arg2).id);
		startActivity(intent);
		
	}
}
